package com.woniu.micropropertyserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.DeployTenement;
import com.woniu.microcommonsserver.entity.RenterInfoEntity;
import com.woniu.microcommonsserver.entity.TenementFaceCheck;
import com.woniu.micropropertyserver.service.ITenementFaceService;
import com.woniu.micropropertyserver.util.DateUtil;
import com.woniu.micropropertyserver.util.PageRest;
import com.woniu.micropropertyserver.util.ResultMsg;
import com.woniu.micropropertyserver.util.StringToDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：bb
 * @date ：Created in 2021/1/28 0029
 * @description：
 * @modified By：
 * @version: $
 */
@RequestMapping("/teface")
@RestController
public class TenementFaceController {

    @Autowired
    private ITenementFaceService  tenementFaceServiceImpl;

    @GetMapping(value = "/findByparams",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findByparams(PageRest pageRest , DeployTenement deployTenement){
        System.out.println(deployTenement);

        PageInfo<DeployTenement> infos= tenementFaceServiceImpl.findByParams(pageRest,deployTenement);
        return new ResultMsg(200,"操作成功",infos);
    }
    @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg save(@RequestBody  TenementFaceCheck tenementFaceCheck){
        System.out.println(tenementFaceCheck);
        int res = tenementFaceServiceImpl.saveInfo(tenementFaceCheck);
      if(res>0){
            return new ResultMsg(200,"操作成功",null);
        }else{
            return new ResultMsg(200,"新增失败",null);
        }

    }

    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResultMsg update(@RequestBody Map map) throws ParseException {

        System.out.println(map);
        int res=0;
        Long[] ids = new Long[((ArrayList) map.get("ids")).size()];
        Date authorizeStartDate = null;
        Date authorizeEndDate = null;
        if (map != null) {
            authorizeStartDate = DateUtil.strToDate((String) map.get("authorizeStartDate"), "yyyy-MM-dd");
            authorizeEndDate = DateUtil.strToDate((String) map.get("authorizeEndDate"), "yyyy-MM-dd");

            System.out.println(map.get("ids"));
            ArrayList strs = (ArrayList) map.get("ids");
            System.out.println("尺寸：" + strs.size());
            for (int i = 0; i < strs.size(); i++) {
                ids[i] = Long.parseLong(strs.get(i).toString());

            }
            System.out.println("日期");
            System.out.println(authorizeStartDate);
            System.out.println(ids);
        /*      JSONArray obj =  JSONArray.parseArray(ids);
        System.out.println(obj);
        Long[] idss=null;
      if(obj.size()>0){
          for(int i = 0;i<=obj.size();i++){
              idss[i]=Long .parseLong((String) obj.get(i));
              System.out.println(idss);
          }
      }
        System.out.println(authorizeStartDate);
        Date authorizeStart= DateUtil.strToDate(authorizeStartDate,"yyyy-MM-dd");
        Date authorizeEnd=DateUtil.strToDate(authorizeEndDate,"yyyy-MM-dd");
     System.out.println(authorizeStart);
*/
             res = tenementFaceServiceImpl.updateAuth(ids, authorizeStartDate, authorizeEndDate);


        }
        if (res > 0) {
            return new ResultMsg(200, "操作成功", null);
        } else {
            return new ResultMsg(200, "更新失败", null);
        }
    }
    @PutMapping(value = "/cancel",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg cancelInfo(Long[] ids){
        System.out.println(ids);
        int res = tenementFaceServiceImpl.delInfo(ids);
        if(res >0){
            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"删除失败",null);
        }

    }
}
